#include <Windows.h>
#include <Zaf\Mwl\Mwl.h>
#include "TestCheckBoxForm.h"
#include "TestComboBoxForm.h"
#include "TestGroupBoxForm.h"
#include "TestLabelForm.h"
#include "TestListBoxForm.h"
#include "TestPanelForm.h"
#include "TestRadioButtonForm.h"
#include "TestTextBoxForm.h"
#include "TestTabForm.h"
#include "TestListViewForm.h"
#include "resource.h"

using namespace Zaf::Mwl;


class MainForm;


struct BaseFormWrapper {
	virtual void Show(MainForm&) = 0;
};

template<typename FormType>
struct FormWrapper : public BaseFormWrapper {

	void Show(MainForm& mainForm) {
			
		FormType form;
		form.SetOwner(mainForm);
		form.Create();
		form.ShowModal();
	}
};



struct ButtonInfo {

	const wchar_t* const Text;
	BaseFormWrapper* Wrapper;

} ButtonInfos[] = {

	L"Test CheckBox", new FormWrapper<TestCheckBoxForm>(),
	L"Test Label", new FormWrapper<TestLabelForm>(),
	L"Test ReadioButton", new FormWrapper<TestRadioButtonForm>(),
	L"Test TextBox", new FormWrapper<TestTextBoxForm>(),
	L"Test ListBox", new FormWrapper<TestListBoxForm>(),
	L"Test ComboBox", new FormWrapper<TestComboBoxForm>(),
	L"Test GroupBox", new FormWrapper<TestGroupBoxForm>(),
	L"Test Panel", new FormWrapper<TestPanelForm>(),
	L"Test Tab", new FormWrapper<TestTabForm>(),
	L"Test ListView", new FormWrapper<TestListViewForm>(),
};


class MainForm : public Form {

public:
	MainForm() {

		CreatedEvent.Register(this, &MainForm::OnCreated);
		m_buttons = new Button[sizeof(ButtonInfos) / sizeof(ButtonInfo)];
	}

	void OnCreated(CreatedEventArgs&) {

		int width = 150;
		int height = 30;

		int x = 0;
		int y = 0;

		for (int index = 0; index < sizeof(ButtonInfos) / sizeof(ButtonInfo); ++index) {

			m_buttons[index].Create();
			m_buttons[index].SetText(ButtonInfos[index].Text);
			m_buttons[index].SetSize(width, height);
			m_buttons[index].SetPosition(x, y);
			m_buttons[index].IsVisible(true);
			m_buttons[index].SetParent(*this);

			y += height;

			if (y > 600) {
				x += width;
			}
		}
	}



	BOOL DlgProc(const WndMsg& msg) {

		switch (msg.Message) {

		case WM_COMMAND: {
			if (msg.WParamHigh == BN_CLICKED) {
				
				wchar_t buffer[256];
				GetWindowText((HWND)msg.LParam, buffer, 256);

				for (int index = 0; index != sizeof(ButtonInfos) / sizeof(ButtonInfo); ++index) {

					if (wcscmp(buffer, ButtonInfos[index].Text) == 0) {
						ButtonInfos[index].Wrapper->Show(*this);
					}
				}

				return TRUE;
			}
		}
		}

		return Form::DlgProc(msg);
	}

private:
	Button* m_buttons;
};



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

	MainForm mainForm;

	mainForm.Create();
	mainForm.SetSize(800, 600);
	mainForm.SetPosition(0, 0);
	mainForm.SetText(L"MainForm");

	mainForm.Show();

	Application::Run(mainForm);
}

